﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class TitleOnsiteTraningRepo
    {
        public const string m_msg_head = "Title OnsiteTraning";

        public IEnumerable<TitleOnsiteTraningModels> GetTitleOnsiteTraning()
        {
            List<TitleOnsiteTraningModels> TitleOnsiteTranings = new List<TitleOnsiteTraningModels>();
            string SQLCommand = @"select * from TBTitleOnsiteTraning WHERE Status <> 'Delete' Order by TitleOnsiteTraningId";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        TitleOnsiteTraningModels TitleOnsiteTraning = new TitleOnsiteTraningModels();
                        TitleOnsiteTraning.TitleOnsiteTraningId = (int)reader["TitleOnsiteTraningId"];
                        TitleOnsiteTraning.TitleOnsiteTraningName = (string)reader["TitleOnsiteTraningName"];
                        TitleOnsiteTraning.Status = (string)reader["Status"];

                        TitleOnsiteTranings.Add(TitleOnsiteTraning);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return TitleOnsiteTranings.ToList();
        }

        public TitleOnsiteTraningModels GetTitleOnsiteTraningById(int id)
        {
            TitleOnsiteTraningModels TitleOnsiteTraning = new TitleOnsiteTraningModels();
            string SQLCommand = @"select * from TBTitleOnsiteTraning where TitleOnsiteTraningId=@TitleOnsiteTraningId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@TitleOnsiteTraningId", id);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        TitleOnsiteTraning.TitleOnsiteTraningId = (int)reader["TitleOnsiteTraningId"];
                        TitleOnsiteTraning.TitleOnsiteTraningName = (string)reader["TitleOnsiteTraningName"];
                        TitleOnsiteTraning.Status = (string)reader["Status"];

                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0002", ex));
                }
            }
            return TitleOnsiteTraning;
        }

        public bool Create(TitleOnsiteTraningModels TitleOnsiteTranin)
        {
            bool result = true;
            string SQLCommand = @"INSERT into TBTitleOnsiteTraning
                                    values (@TitleOnsiteTraningName,@Status)";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleOnsiteTraningName", TitleOnsiteTranin.TitleOnsiteTraningName);
                    db.AddParameter("@Status", "Active");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Edit(TitleOnsiteTraningModels TitleOnsiteTranin)
        {
            bool result = true;
            string SQLCommand = @"Update TBTitleOnsiteTraning 
                                    Set TitleOnsiteTraningName=@TitleOnsiteTraningName
                                    where TitleOnsiteTraningId=@TitleOnsiteTraningId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleOnsiteTraningId", TitleOnsiteTranin.TitleOnsiteTraningId);
                    db.AddParameter("@TitleOnsiteTraningName", TitleOnsiteTranin.TitleOnsiteTraningName);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Delete(TitleOnsiteTraningModels TitleOnsiteTranin)
        {
            bool result = true;
            string SQLCommand = @"Update TBTitleOnsiteTraning
                                    Set Status=@Status
                                    where TitleOnsiteTraningId=@TitleOnsiteTraningId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleOnsiteTraningId", TitleOnsiteTranin.TitleOnsiteTraningId);
                    db.AddParameter("@Status", "Delete");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0005", ex));
                    return false;
                }
            }
            return result;
        }
    }
}